package com.artron.mediaartron.util;

import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.widget.TextView;

import com.artron.baselib.utils.UIUtils;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-08-16.
 */

public class TextSpanUtils {

    public static class TextSpan {
        private String text;
        private Object span;

        private TextSpan(String text, Object span) {
            this.text = text;
            this.span = span;
        }

        public static TextSpan holder(String text, Object span) {
            return new TextSpan(text, span);
        }

        private String getText() {
            return text;
        }

        private Object getSpan() {
            return span;
        }
    }


    /**
     * 抽取的工具方法
     * 这个方法builder.append(text)时Span.FLAG会作用于这个text，
     * 那么说明EditText中添加文字的其实就是取出来Text，然后添加文字
     * 来实现的。我这里的效果等同于自动在EditText中添加文字
     */
    public static CharSequence getTextSpan(TextSpan... textSpanArr) {
        SpannableStringBuilder builder = new SpannableStringBuilder();
        int start = 0;
        int end;
        for (TextSpan textSpan : textSpanArr) {
            String text = textSpan.getText();
            end = start + text.length();
            builder.append(text);
            builder.setSpan(textSpan.getSpan(), start, end,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            start += text.length();
        }
        return builder;
    }

    public static void setTextWithSpan(TextView tv, TextSpan... textSpan) {
        tv.setText(getTextSpan(textSpan));
    }

}
